/*
 * @Descripttion:
 * @version:
 * @Author: zhaoxiaodong
 * @Date: 2020-08-31 14:34:33
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-03-15 13:43:04
 */
const DistInitPlugin = require('cloud-common/config/plugin/DistInitPlugin.js');
const DistEndPlugin = require('cloud-common/config/plugin/DistEndPlugin.js');
const FailedPlugin = require('cloud-common/config/plugin/FailedPlugin.js');
const DonePlugin = require('cloud-common/config/plugin/DonePlugin.js');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
// const HtmlPlugin = require('cloud-common/config//plugin/HtmlPlugin.js');
const devServer = require('cloud-common/config/devServer.js');
const urlConfig = require('./config/common/urlConfig.js'),
  version = new Date().getTime(),
  publicPath = process.env.PACK_CDN
    ? urlConfig[process.env.mode]['CDN_URL']
    : './';
const path = require('path');
const { name, port } = require('./package');
function resolve(dir) {
  return path.join(__dirname, dir);
}
const vueConfig = {
  runtimeCompiler: true,
  lintOnSave: false, // eslint关闭
  productionSourceMap: false, // map 映射
  // publicPath: `//localhost:${port}` ,
  publicPath: publicPath,
  chainWebpack: config => {
    config.plugin('define').tap(args => {
      args[0].URL_CONFIG = JSON.stringify(urlConfig[process.env.mode]);
      return args;
    });
    config.module
      .rule('fonts')
      .use('url-loader')
      .loader('url-loader')
      .options({
        limit: 4096, // 小于4kb将会被打包成 base64
        fallback: {
          loader: 'file-loader',
          options: {
            name: 'fonts/[name].[hash:8].[ext]',
            publicPath
          }
        }
      })
      .end();
    config.module
      .rule('images')
      .use('url-loader')
      .loader('url-loader')
      .options({
        limit: 4096, // 小于4kb将会被打包成 base64
        fallback: {
          loader: 'file-loader',
          options: {
            name: 'img/[name].[hash:8].[ext]',
            publicPath
          }
        }
      });
  },

  configureWebpack: {
    plugins: [
      new DistInitPlugin(),
      new DistEndPlugin(),
      // new HtmlPlugin(),
      new FailedPlugin(),
      new DonePlugin()
    ],
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    optimization: {
      minimizer: [
        new UglifyJsPlugin({
          uglifyOptions: {
            compress: {
              // warnings: false,
              drop_console: true, //console
              drop_debugger: false,
              pure_funcs: ['console.log'] //移除console
            }
          }
        })
      ]
    },
    output: {
      // 把子应用打包成 umd 库格式
      library: `${name}`,
      libraryTarget: 'umd',
      jsonpFunction: `webpackJsonp_${name}`,
      filename: `static/js/[name]_[hash]_${version}.js?`,
      chunkFilename: `static/js/[name]_[hash]_${version}.js`
    }
  },
  outputDir: 'dist',
  devServer: devServer,
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          javascriptEnabled: true,
          math: 'always'   // 此处指定为兼容 less-loader 3.x 的默认选项
        }
      }
    },
  },
  transpileDependencies: ['cloud-common','cloud-components', 'ant-design-vue']
};

module.exports = vueConfig;
